#include <iostream>
#include <unistd.h>
#include "RingQueue.hpp"

void *consumer(void *args)
{
    RingQueue<int> *rq = static_cast< RingQueue<int> *>(args);
    while (true)
    {
        sleep(1);
        int t = 0;
        rq->Dequeue(&t);
        //std::cout << "处理了一个任务：" << t.GetA() << "+" << t.GetB() 
        //            << "=" << t.Result() << std::endl;
        std::cout << "处理了一个数据：" << t << std::endl;
    }
    return nullptr;
}

void *producer(void *args)
{
    //int a = 1, b =2;
    int data = 1;
    RingQueue<int> *rq = static_cast< RingQueue<int> *>(args);
    while (true)
    {
        //sleep(1);
        //Task t(a, b);
        //bq->EQueue(t);
        rq->Equeue(data);
        //std::cout << "生产了一个任务：" << a << "+" << b << "=?" << std::endl;
        std::cout << "生产了一个数据：" << data << std::endl;
        
        data++;
        //a++, b++;
    }
    return nullptr;
}

int main()
{
    // 申请阻塞队列
    RingQueue<int> *rq = new RingQueue<int>();
    // 构建生产者消费者关系
    pthread_t c[2], p[3];

    pthread_create(c , nullptr, consumer, rq);
    pthread_create(c + 1, nullptr, consumer, rq);
    pthread_create(p, nullptr, producer, rq);
    pthread_create(p + 1, nullptr, producer, rq);
    pthread_create(p + 2, nullptr, producer, rq);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(p[0], nullptr);
    pthread_join(p[1] + 1, nullptr);
    pthread_join(p[2] + 2, nullptr);

    return 0;
}